<?php

namespace app\home\controller;

use \think\Db;

class Up
{

	public function __construct()
	{

	}

	public function trade_rates()
	{
		$list = Db::name('merchant')
			->alias('m')
			->join('agent a', 'a.agent_id = m.agent_id', 'LEFT')
			->join('agent_level al', 'a.level_id = al.level_id', 'LEFT')
			->join('merchant_weixin mw', 'm.merchant_id = mw.merchant_id', 'LEFT')
			->join('merchant_alipay ma', 'm.merchant_id = ma.merchant_id', 'LEFT')
			->where('m.check_status', '=', '-1')
			->field('m.merchant_id, m.agent_id, m.status, m.merchant_no, m.merchant_name, al.trade_rates as agent_rates, m.trade_rates, mw.sub_mch_id, ma.app_auth_token, mw.sub_gate as gate_weixin, ma.sub_gate as gate_alipay, mw.status as status_weixin, ma.status as status_alipay')
			->select();
		foreach($list as $key => $val) {
			Db::name('trade')->where('merchant_id', '=', $val['merchant_id'])->update([
				'agent_rates' => $val['agent_rates'],
				'trade_rates' => $val['trade_rates'],
			]);
		}
	}

	public function user_card()
	{

		$list = Db::name('mch_user')->where('card_no', '=', '')->select();
		foreach($list as $key => $val) {
			if(!empty($val['phone'])) {
				Db::name('mch_user')->where('id', '=', $val['id'])->update([
					'card_no' => $val['phone'],
				]);
			} else {
				Db::name('mch_user')->where('id', '=', $val['id'])->update([
					'card_no' => model('MchUser')->make_card(),
				]);
			}
		}
	}

}

